library(magrittr)
library(stringr)
library(plyr)
library(tidyverse)
library(stargazer)


rm(list=ls())
home = 'C:/Users/Jason/Dropbox/VNA_Responsiveness/Analysis/JOP-dataverse/'


permutation.results = paste0(home, 'RI-analyses.Rds') %>%
  readRDS
experimental.results = paste0(home, 'experimental-analyses.Rds') %>%
  readRDS


columns = c('tab5col1','tab5col2','tab5col3','tab5col4','tab5col5','tab5col6')
significance = subset(permutation.results, 
                      Result %in% columns) %>%
  dlply(.variables='Result', 
        .fun=function(column) {
          ddply(.data=column,
                .variables='term',
                .fun=function(x) {
                  est = subset(experimental.results, 
                               Result==x$Result[1] & term==x$term[1])$estimate
                  ris = x$estimate
                  qest = ecdf(ris)
                  Q = qest(est) %>%
                    round(digits=3) %>%
                    str_pad(width=5, side='right', pad=0) %>%
                    paste0('(', ., ')')
                  est = est %>%
                    round(digits=3) %>%
                    str_pad(width=5, side='right', pad=0)
                  data.frame(output=paste0(est, '\n', Q),
                             stringsAsFactors=F) %>%
                    set_colnames(x$Result[1])
                }, .inform=T)
        }, .progress='text', .inform=T)


output = merge(significance[['tab5col1']], significance[['tab5col2']], by='term', all=T) %>%
  merge(significance[['tab5col3']], by='term', all=T) %>%
  merge(significance[['tab5col4']], by='term', all=T) %>%
  merge(significance[['tab5col5']], by='term', all=T) %>%
  merge(significance[['tab5col6']], by='term', all=T) %>%
  set_rownames(.[,'term']) %>%
  subset(select=-term)
output = output[c('Citizen',
                  'Firm',
                  'FullTime',
                  'CentNom',
                  'Competitive',
                  '(Intercept)'),] %>%
  adply(.margins=1, .fun=function(x) {
    x1 = str_extract(string=unlist(x), pattern='.*(?=\\n)')
    x2 = str_extract(string=unlist(x), pattern='(?<=\\n).*')
    rbind(x1, x2) %>%
      set_colnames(colnames(x))
  }) %>%
  sapply(FUN=as.character) %>%
  as.data.frame(stringsAsFactors=F)


c(subset(experimental.results, Result=='tab5col1')$N[1],
  subset(experimental.results, Result=='tab5col2')$N[1],
  subset(experimental.results, Result=='tab5col3')$N[1],
  subset(experimental.results, Result=='tab5col4')$N[1],
  subset(experimental.results, Result=='tab5col5')$N[1],
  subset(experimental.results, Result=='tab5col6')$N[1]) %>%
  matrix(nrow=1) %>%
  as.data.frame %>%
  set_colnames(colnames(output)) %>%
  rbind(output, .) %>%
  set_rownames(c('% Citizen','2',
                 '% Firm','5',
                 '% Full-time','7',
                 '% Central nominee','8',
                 '% Competitive','9',
                 'Constant','10',
                 'Observations')) %>%
  stargazer(summary=F, 
            header=F,
            align=T,
            notes.append=F,
            notes='Randomization inference based on 10,000 randomizations. Quantile of experimental estimate in parentheses. The unit of analysis is the province.',
            title='Speech content analyses.')

